package com.akw.web.biz;

import com.akw.web.bean.Person;
import jakarta.servlet.ServletException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.function.ServerRequest;
import org.springframework.web.servlet.function.ServerResponse;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 专门处理user相关的业务
 */
@Service
@Slf4j
public class UserBizHandler {

    public ServerResponse getUser(ServerRequest request) throws Exception {
        String id = request.pathVariable("id");
        log.info("获取用户: {}", id);
        Person person = new Person(1L, "AKW", "2497744746@qq.com", 18);
        // 构造响应
        return ServerResponse.ok().body(person);
    }

    public ServerResponse list(ServerRequest request) {
        List<Person> list = Arrays.asList(
                new Person(1L, "AKW", "2497744746@qq.com", 18),
                new Person(1L, "AKW", "2497744746@qq.com", 18),
                new Person(1L, "AKW", "2497744746@qq.com", 18)
        );
        return ServerResponse.ok().body(list);  // 凡是body中的对象, 就是以前@ResponseBody原理, 利用HttpMessageConverter 写出为json
    }

    public ServerResponse addUser(ServerRequest request) throws ServletException, IOException {
        Person body = request.body(Person.class);
        log.info("添加用户: {}", body);
        Person person = new Person(1L, "AKW", "2497744746@qq.com", 18);
        log.info("添加用户: {}", person);
        // 构造响应
        return ServerResponse.ok().body(person);
    }

    public ServerResponse updateUser(ServerRequest request) {
        String id = request.pathVariable("id");
        log.info("更新用户: {}", id);
        Person person = new Person(1L, "AKW", "2497744746@qq.com", 18);
        log.info("更新用户: {}", person);
        return null;
    }

    public ServerResponse deleteUser(ServerRequest request) {
        String id = request.pathVariable("id");
        log.info("删除用户: {}", id);
        return null;
    }
}
